/* $RCSFile: PageThemeManager.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/Design/PageThemeManager.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:28  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Web.Caching;
using System.Diagnostics;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Serialization;
using System.Text;
using System.IO;
using System.Globalization;

using AddonNice.Core;
using AddonNice.Helpers;
using AddonNice.Diagnostics;
using AddonNice.Settings.Cache;

namespace AddonNice.Design
{
    /// <summary>
    /// The PageThemeManager class encapsulates all data logic necessary to
    /// use differents page themes across the entire portal.
    /// </summary>
	public class PageThemeManager
    {
        public PageThemeManager()
        {}

        /// <summary>
        /// Clears cache for Net 2 Themes
        /// </summary>
		public void ClearCacheList()
		{
            CurrentCache.Remove(Key.Net2ThemeList);
		}


        /// <summary>
        /// Read the Path dir and returns an ArrayList with all the Themes found.
        /// Static because the list is Always the same.
        /// </summary>
        /// <returns></returns>
        public static ArrayList GetAllNet2Themes()
        {
            ArrayList lst               = null;
            string strk                 = Key.Net2ThemeList;
            if (!CurrentCache.Exists(strk))
            {
                string wPath            =   Path;

                string[] themes;

                // Try to read directories from public theme path
                try
                {
                    // Try to read directories from public theme path
                    if (Directory.Exists(wPath))
                    {
                        themes          =   Directory.GetDirectories(wPath);
                        if ( PageThemesTrace.Sw.TraceVerbose )
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageThemeManager GetAllNet2Themes wPath:{0}, NbDirectories:{1}", wPath, themes.Length),PageThemesTrace.Sw.Info);
                    }
                    else
                    {
                        themes          =   new string[0];
                    }
                    // remove path part
                    lst                 =   new ArrayList();
                    for (int i = 0; i < themes.Length; i++)
                    {
                        string str      =   System.IO.Path.GetFileName(themes[i]);
                        string lnm      =   str.ToLower();
                        if ( lnm.IndexOf("help") != -1 || //Ignore Help theme
                            lnm.IndexOf('$') != -1 || // Ignore any theme with $ in name
                            lnm.IndexOf("commonimg") != -1 || // Ignore CommonImg
                            lnm.IndexOf("error") != -1 )  // Ignore Error error
                            continue;
                        if ( PageThemesTrace.Sw.TraceVerbose )
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageThemeManager GetAllNet2Themes Adding ModulesLayout:{0}-{1}",themes[i],str),PageThemesTrace.Sw.Info);
                        NameListItem t  =   new NameListItem(str);
                        lst.Add(t);
                    }

                }
                // CS 19/3/4				
                catch (Exception ex)
                {
                    if ( PageThemesTrace.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]GetAllNet2Themes Directory.GetDirectories'{0}' failed, no themes to load, ex : {1}", Path, ex),PageThemesTrace.Sw.Info);
                }

                if ( lst.Count > 0)
                    CurrentCache.Insert(strk, lst);
            }
            else
            {
                lst = (ArrayList)CurrentCache.Get(strk);
            }
            // to avoid side effect of adding repetively elements in the shared list from cache
            return (ArrayList)lst.Clone();
        }

		/// <summary>
		/// The virtual path of the NET 2 Themes 
		/// </summary>
		public static string WebPath
		{
			get
			{
                return "~/App_Themes";
			}
		}
		/// <summary>
		/// The physical path of the NET 2 ModulesLayout dir 
		/// </summary>
		public static string Path
		{
			get
			{
                return HttpContext.Current.Server.MapPath(WebPath);
			}
		}
    }
}